home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / tpa2_a.arc / DOSEXEC.PAS < prev    next >
Pascal/Delphi Source File  |  1991-04-28  |  5KB  |  129 lines

  1. {$M $4000,0,0}
  2. {═══════════════════════════ DOSEXEC.PAS ════════════════════════════}
  3. { Usage:  Dosexec            (From Editor, just Run)                 }
  4. {   (prompts for Program to run)                                     }
  5. {═══════════════════════════ DOSEXEC.PAS ════════════════════════════}
  6.  
  7. {- This demonstration illustrates the use of "local" CSeg assembly   }
  8. {- variables allocated in the 1st Block of the current Code Segment. }
  9. {- This is a powerful but somewhat complicated technique that you    }
  10. {- may NEVER need to use.  (It is useful for creating customized     }
  11. {- Exec Procedures, and Interrupt Routines that can 'Chain' rather   }
  12. {- than Iret).  For now, just run the demonstration, and keep in     }
  13. {- the back of your mind that variables CAN be stored in the Code    }
  14. {- segment if you should ever find it necessary.                     }
  15.  
  16. {- NOTE: this demonstration program does not parse the parameters    }
  17. {-  into the 'default FCBs', so some programs (including diskcopy,   }
  18. {-  format, chkdsk, etc) cannot be run directly.  However, using     }
  19. {-  example 2 below, you can shell to DOS and run these programs.    }
  20.  
  21. {═════════════════════════════ Examples ═════════════════════════════}
  22. {  ( <CR> signifies Carriage Return )                                }
  23. {  1. To list a directory on a system with a bootable hard disk:     }
  24. {      At the 'Path' prompt:         --> C:\command.com<CR>          }
  25. {      At the 'Parameters' prompt:   --> /c dir<CR>                  }
  26. {  2. To shell to DOS with a system disk in drive A:                 }
  27. {      At the 'Path' prompt:         --> A:\command.com<CR>          }
  28. {      At the 'Parameters' prompt:   --> <CR>                        }
  29. {      ( Type   EXIT<CR>   to return to Turbo )                      }
  30. {═════════════════════════════ Examples ═════════════════════════════}
  31.  
  32.  
  33. {════════════════════════════ Exec ═════════════════════════════}
  34. { Execute specified Path and Command Line or return Error in    }
  35. { global VAR DosError.  Compatable with DOS Unit Exec Procedure }
  36. {════════════════════════════ Exec ═════════════════════════════}
  37. VAR DosError: Integer;
  38. PROCEDURE Exec(Path,CmdLine: String);
  39. VAR
  40. {- local var addr are assigned in reverse order -}
  41. FCB6C:   Pointer;   { Dword pointer to FCB2 (6CH in PSP) }
  42. FCB5C:   Pointer;   { Dword pointer to FCB1 (5CH in PSP) }
  43. CmdPtr:  Pointer;   { Dword pointer parameter string     }
  44. EnvSeg:  WORD;      { Segment address of environment     }
  45.  
  46. PathPtr: Pointer;   { Dword pointer Path to Exec         }
  47.  
  48. BEGIN
  49. Assemble
  50. Jmp Begin
  51. SaveSs Dw 0           ; Ss and Sp MUST be saved in our Code Segment
  52. SaveSp Dw 0           ; Int $21 Function $4B does not restore them
  53. Begin:
  54.   Push Ds,Bp
  55.   Mov SaveSs,Ss       ; TP&Asm automatically supplies the needed
  56.   Mov SaveSp,Sp       ;  Cs overrides for SaveSs and SaveSp
  57.    ;- You can code the override explicitly (Cs Mov SaveSs,Ss) if you
  58.    ;- prefer, and you can disable Presumptions for "WYSIWYG" assembly
  59.  
  60.   Mov W CmdPtr+2,Ss   ; Save Command Line segment
  61.   Lea AX,CmdLine
  62.   Mov W CmdPtr,Ax     ; And offset in Parameter Block.
  63.  
  64. ;- Technical Note: For String Value Parameters, Turbo 4/5 pushes
  65. ;-  a Pointer to a Copy of the String Value.  The pointer will
  66. ;-  reference either the Local Copy OR a "Packed" copy in the
  67. ;-  callers Code Segment.  Since we will be modifying the Path
  68. ;-  string (to add a zero byte), it is essential that we use
  69. ;-  the Local copy and not a Code Seg copy.  For this reason
  70. ;-  we do NOT want to use the Pointers at [Bp+4] and [Bp+8].
  71. ;-  Instead, take advantage of the fact that TP&Asm allows
  72. ;-  reference to parameters BY NAME - the resulting code is
  73. ;-  easier to understand, and Value parameters referenced by
  74. ;-  name will ALWAYS refer to a local copy in the Stack segment
  75.  
  76.   Push Ss             ; Point ES:BX to Parameter Block
  77.   Pop Es
  78.   Lea Bx,EnvSeg       ; Use LEA since EnvSeg is implicitly indexed (by BP)
  79.   Mov EnvSeg,0        ; Pass a copy of the current enviroment
  80.  
  81.   Push Ss
  82.   Pop Ds
  83.   Lea Si,Path         ; Point DS:SI to length byte of LOCAL COPY
  84.  
  85.   Xor Ax,Ax
  86.   Mov Al,[Si]         ; Get length
  87.   Inc Si
  88.   Mov Dx,Si           ; Point DS:DX to program name
  89.   Add Si,Ax           ; Point past Last Char
  90.   Mov b[Si],0         ; Make it AsciiZ in LOCAL COPY
  91.  
  92.   Mov Ax,4B00h        ; DOS EXEC function: al=0, ah=4Bh
  93.   Int 21h             ; Execute
  94.   IF NC Xor Al,Al     ; If successful (No Carry), clear error code
  95.                       ; 'IF' is an A86 specialty, supported for compatibility
  96.   Xor Ah,Ah
  97.  
  98.   Cli
  99.   Mov Ss,SaveSs       ; restore our Stack
  100.   Mov Sp,SaveSp
  101.   Sti
  102.   Pop Bp,Ds           ; Restore Turbo Bp and Ds
  103.   Mov DosError,Ax     ; Store DosError AFTER restoring DS
  104.  
  105. end; {Assemble}
  106.  
  107. END; {Exec}
  108.  
  109.  
  110. VAR Path,CmdLine: String;
  111.  
  112. {════════════════════ Demonstrate Exec Call ════════════════════}
  113. BEGIN {Main Program}
  114.   WRITELN('Path and Program to execute [e.g. C:\Command.com]');
  115.   WRITE('--> '); READLN(Path);
  116.   WRITELN('Parameters for executing program [e.g. /C Dir]');
  117.   WRITE('--> '); READLN(CmdLine);
  118.   WRITELN('Before Exec');
  119.  
  120.   Exec(Path,CmdLine);
  121.  
  122.   CASE DosError OF
  123.     0:  WRITELN('Successful Exec');
  124.     2:  WRITELN('File ',Path,' Not Found');
  125.     8:  WRITELN('Not Enough Memory to Exec ',Path);
  126.    ELSE WRITELN('Exec Failed: Dos Error ',DosError);
  127.   END; {CASE DosError}
  128. END.
  129.